package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：lintao
 * @date ：Created in 2020/11/30
 * @description ：预约设置业务层接口实现类
 * @version: 1.0
 */
@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量添加
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList!=null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                // 检查此数据（日期）是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count >0){
                    // 已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    // 不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    // 根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) { // 2020-11
        String dateBegin = date + "-1";  // 2020-11-1
        String dateEnd = date+"-31";  // 2020-11-31
        Map<String,String> map = new HashMap<String, String>();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<Map>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());  // 获得日期
            orderSettingMap.put("number",orderSetting.getNumber()); // 获得可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());  // 获得已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        // 根据日期查询当前日期是否已设置
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            // 修改
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            // 添加
            orderSettingDao.add(orderSetting);
        }
    }
}
